package com.tadpole.music.presenter.home;

import com.tadpole.music.bean.home.GoClockInBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.home.ClockInIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 打卡
 * Created By 孙岩
 * on 2020/12/10
 */
public class ClockInPresenter extends BasePresenter<ClockInIView> {
    @SuppressWarnings("unchecked")
    public void goClockIn() {
        if (isViewAttached()) {
            String url = UrlConfig.goClockIn;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.POST_HEAD_REQUEST_NO)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));

                                    JSONObject sayingsObj = new JSONObject(object.getString("sayings"));
                                    GoClockInBean.DataBean.SayingsBean sayingsBean = new GoClockInBean.DataBean.SayingsBean(
                                            JsonParseUtil.getStr(sayingsObj, "author"),
                                            JsonParseUtil.getStr(sayingsObj, "content")
                                    );

                                    JSONObject signObj = new JSONObject(object.getString("sign_data"));
                                    GoClockInBean.DataBean.SignDataBean signDataBean = new GoClockInBean.DataBean.SignDataBean(
                                            JsonParseUtil.getInt(signObj, "id"),
                                            JsonParseUtil.getStr(signObj, "created_at"),
                                            JsonParseUtil.getStr(signObj, "updated_at"),
                                            JsonParseUtil.getInt(signObj, "user_id"),
                                            JsonParseUtil.getInt(signObj, "continue_sign_days"),
                                            JsonParseUtil.getInt(signObj, "total_sign_days")
                                    );

                                    JSONObject userObj = new JSONObject(object.getString("user"));
                                    GoClockInBean.DataBean.UserBean userBean = new GoClockInBean.DataBean.UserBean(
                                            JsonParseUtil.getInt(userObj, "age"),
                                            JsonParseUtil.getStr(userObj, "avatar"),
                                            JsonParseUtil.getStr(userObj, "nickname")
                                    );

                                    JSONObject shareObj = new JSONObject(object.getString("share"));
                                    GoClockInBean.DataBean.ShareBean shareBean = new GoClockInBean.DataBean.ShareBean(
                                            JsonParseUtil.getStr(shareObj, "desc"),
                                            JsonParseUtil.getStr(shareObj, "thumb"),
                                            JsonParseUtil.getStr(shareObj, "title"),
                                            JsonParseUtil.getStr(shareObj, "url")
                                    );

                                    GoClockInBean.DataBean dataBean = new GoClockInBean.DataBean(
                                            JsonParseUtil.getStr(object, "erweima"), sayingsBean, signDataBean, userBean,shareBean
                                    );
                                    getView().showClockInResult(dataBean);

                                } else if (obj.getInt("code") == 202) {
                                    getView().show202(obj.getString("msg"));
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
